package student.world;

/**
 * An instance of Location is an ordered pair (row, col).
 */
public class Location {
	
	private CritterWorld world;
	private int row;
	private int col;
	
	public Location(CritterWorld w, int r, int c) {
		world = w;
		row = r;
		col = c;
	}
	
	public int getRow() {
		return row;
	}
	
	public int getCol() {
		return col;
	}
	
	/**
	 * Checks whether this Location if a valid Hex location on this Location's world.
	 * @return true if this Location is valid.
	 */
	public boolean isValid() {
		// Checks if this Location if off the East Side.
		if (col > world.MAX_COLUMN){
//			System.out.println("Location " + row + ", " + col + " is off the East side");
			return false;
		}
		// Checks if this Location if off the West Side.
		if (col < 0){
//			System.out.println("Location " + row + ", " + col + " is off the West side");
			return false;
		}
		// Checks if this Location if off the South Side.
		if (col > 2*row){
//			System.out.println("Location " + row + ", " + col + " is off the South side");
			return false;
		}
		// Checks if this Location if off the North Side.
		if (col + world.ROW_COL_EXPR < 2*row){
//			System.out.println("Location " + row + ", " + col + " is off the North side");
			return false;
		}
		return true;
	}
	
	/**
	 * Returns the next Location in the given direction. The Location may be invalid.
	 * @param direction the direction to look for the next Location
	 * @return the next Location in the appropriate direction.
	 */
	public Location nextLocation(int direction) {
		Location newLoc = null;
		switch(direction) {
		case 0: newLoc = new Location(world, row + 1, col); break;
		case 1: newLoc = new Location(world, row + 1, col + 1); break;
		case 2: newLoc = new Location(world, row, col + 1); break;
		case 3: newLoc = new Location(world, row - 1, col); break;
		case 4: newLoc = new Location(world, row - 1, col - 1); break;
		case 5: newLoc = new Location(world, row, col - 1); break;
		}
		return newLoc;
	}
	
	/**
	 * Determines if this Location is equal to the given Object.
	 * @param compared the Object this Location is being compared to.
	 * @return true if the given Object is a Location and this Location has the same row and col as the given Object.
	 */
	@Override
	public boolean equals(Object compared) {
		if (compared == null) return false;
		if (!(compared instanceof Location)) return false;
		Location loc = (Location) compared;
		return (loc.row == row && loc.col == col);
	}
	
	@Override
	public int hashCode(){
		return 100000*row + col;
	}
}
